package com.battle.service.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.battle.pojo.dto.role.UserRoleDto;
import com.battle.pojo.entity.PersonMpWarCase;
import com.battle.pojo.enums.PersonMpWarCaseTypeEnum;
import com.battle.pojo.vo.personMpWarCase.GetPersonMpWarCaseDownListVo;
import com.battle.service.mapper.PersonMpWarCaseMapper;
import com.battle.service.repository.in.PersonMpWarCaseRepository;
import nirvana.core.exceptions.BizException;
import nirvana.core.repository.Repository;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import nirvana.core.utils.ids.IdGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 */
@Service
public class PersonMpWarCaseRepositoryImpl extends Repository<PersonMpWarCaseMapper, PersonMpWarCase> implements PersonMpWarCaseRepository {

    private final PersonMpWarCaseMapper mapper;

    public PersonMpWarCaseRepositoryImpl(PersonMpWarCaseMapper mapper) {
        this.mapper = mapper;
    }


    @Override
    public Map<Long, PersonMpWarCase> getMap(List<Long> ids) {
        Map<Long, PersonMpWarCase> map = new HashMap<>();

        if (CollectionUtil.isNotEmpty(ids)) {
            List<PersonMpWarCase> list = listByIds(ids);
            if (CollectionUtil.isNotEmpty(list)) {
                map = list.stream().collect(Collectors.toMap(PersonMpWarCase::getId, pojo -> pojo));
            }
        }
        return map;
    }

    /**
     * 批量获取fid的人员战力表
     * @param fids
     * @return
     */
    @Override
    public Map<Long, List<GetPersonMpWarCaseDownListVo>> getDtoByFids(List<Long> fids) {
        if (CollectionUtils.isEmpty(fids)) {
            return Collections.EMPTY_MAP;
        }

        return mapper.getDtoByFids(fids)
                .stream()
                .collect(Collectors.groupingBy(GetPersonMpWarCaseDownListVo::getFid));

    }

    @Override
    public List<PersonMpWarCase> getByFidAndType(Long fid, PersonMpWarCaseTypeEnum type) {
        //如果fid不等于空就批量查询
        if (fid != null) {
            return mapper.selectList(
                    new LambdaQueryWrapper<PersonMpWarCase>()
                            .eq(PersonMpWarCase::getFid, fid)
                            .eq(PersonMpWarCase::getType, type)
            );
        }
        return Collections.EMPTY_LIST;
    }

    /**
     * 根据人员id查看关联的队伍或者车辆
     * @param personId
     * @return
     */
    @Override
    public List<PersonMpWarCase> getByPerson(Long personId) {
        if (personId != null) {
            return mapper.selectList(
                    new LambdaQueryWrapper<PersonMpWarCase>()
                            .eq(PersonMpWarCase::getPersonId, personId)
            );
        }
        return Collections.EMPTY_LIST;
    }

    /**
     * 根据人员id删除人员战力mp表
     * @param personId
     */
    @Override
    public void removeByPersonId(Long personId) {
        List<PersonMpWarCase> byPerson = getByPerson(personId);
        if (CollectionUtil.isNotEmpty(byPerson)){
            //批量提取id并且真实删除
            List<Long> ids = byPerson.stream().map(PersonMpWarCase::getId).collect(Collectors.toList());
            trueDeletionByIds(ids);
        }
    }

    /**
     * 根据fid和type删除人员战力mp表
     * @param fid
     * @param type
     */
    @Override
    public void removeByFidAndType(Long fid, PersonMpWarCaseTypeEnum type) {
        List<PersonMpWarCase> byFidAndType = getByFidAndType(fid, type);
        if (CollectionUtil.isNotEmpty(byFidAndType)){
            //批量提取id并且真实删除
            List<Long> ids = byFidAndType.stream().map(PersonMpWarCase::getId).collect(Collectors.toList());
            trueDeletionByIds(ids);
        }
    }

    @Override
    public void bathAddPersonMpWarCase(PersonMpWarCaseTypeEnum type, Long fid, List<Long> personIds) {
        if (CollectionUtil.isNotEmpty(personIds)) {
            //循环personIds封装为List<PersonMpWarCase>
            List<PersonMpWarCase> list = personIds.stream().map(personId -> {
                PersonMpWarCase pojo = new PersonMpWarCase();
                pojo.setFid(fid);
                pojo.setPersonId(personId);
                pojo.setType(type);
                pojo.setId(IdGenerator.SNOW.generate());
                //检查人员id是否已经绑定
                List<PersonMpWarCase> byPerson = getByPerson(personId);
                if (CollectionUtil.isNotEmpty(byPerson)) {
                    throw new BizException("人员id已经绑定，一个人员只可在一个队伍");
                }
                return pojo;
            }).collect(Collectors.toList());

            saveBatch(list);
        }
    }
}